//#define __DEBUG__

if (!X_Client) exitWith {};
private ["_array","_control","_display","_ok","_oldpos","_available_drops","_current_distance"];

_available_drops = (["available_drop_vehicles",-1] call XNetGetJIP);
#ifdef __DEBUG__
diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"calldrop.sqf","available_drop_vehicles",(["available_drop_vehicles","empty"] call XNetGetJIP)];
#endif
if ( _available_drops == 0) exitWith {"There are no more transport choppers available!" call XfHQChat};
if (!(["para_available",False] call XNetGetJIP)) exitWith {"Transport chopper in the air... Drop not available!" call XfHQChat};

["arti1_marker_1",position player,"ELLIPSE","ColorYellow",[d_drop_max_dist,d_drop_max_dist],"",0,"","FDiagonal"] call XfCreateMarkerLocal;

x_drop_type = "";
x_drop_vehicle = x_drop_aircraft;
x_drop_display = "";
_oldpos = position X_DropZone;
disableSerialization;
_ok = createDialog "XD_AirDropDialog";
_display = uiNamespace getVariable "D_DROP_DIALOG";
_control = _display displayCtrl 11002;
_array = x_drop_array select 0;
_control ctrlSetText (_array select 0);
if (count x_drop_array > 1) then {
	_control = _display displayCtrl 11003;
	_array = x_drop_array select 1;
	_control ctrlSetText (_array select 0);
} else {
	ctrlShow [11003, false];
	ctrlShow [11004, false];
};
if (count x_drop_array > 2) then {
	_control = _display displayCtrl 11004;
	_array = x_drop_array select 2;
	_control ctrlSetText (_array select 0);
};
#ifdef __DEBUG__
diag_log Format["(%1)[%2]: %3: %4 (%5)",diag_tickTime,"calldrop.sqf","X_Drop_Zone before selection",(getPos X_DropZone), (isnil "X_DropZone")];
#endif
onMapSingleClick "X_DropZone setpos _pos;""x_drop_zone"" setMarkerPos _pos;""x_drop_zone"" setMarkerAlpha 0.4;";

waitUntil {x_drop_type != "" || !dialog || !alive player};
onMapSingleClick "";
if (x_drop_display == "") then {x_drop_display = x_drop_type;};
#ifdef __DEBUG__
diag_log Format["(%1)[%2]: %3: %4 (%5)",diag_tickTime,"calldrop.sqf","X_Drop_Zone after selection",getPos X_DropZone, (isnil "X_DropZone")];
#endif

deleteMarkerLocal "arti1_marker_1";
if (!alive player) exitWith {if (dialog) then {closeDialog 0}};
if (x_drop_type != "") then {
	_current_distance = (player distance [position X_DropZone select 0, position X_DropZone select 1, 0]);
#ifdef __DEBUG__
diag_log Format["(%1)[%2]: %3: %4 (%5)",diag_tickTime,"calldrop.sqf","Player distance to target",_current_distance, d_drop_max_dist];
#endif
	if (_current_distance >= d_drop_max_dist) exitWith {
		(format ["You are too far away from the drop point, no line of sight !!! Get closer (< %2m).",_current_distance, d_drop_max_dist]) call XfHQChat;
		x_dropzone setpos _oldpos;
		"x_drop_zone" setMarkerPos _oldpos;
	};
	[player, format ["%1 requested!", x_drop_display]] call XfSideChat;
	["x_dr_t", [x_drop_type,markerPos "x_drop_zone",player,x_drop_vehicle]] call XNetCallEvent;
} else {
	"Air drop canceled" call XfHQChat;
	x_dropzone setpos _oldpos;
	"x_drop_zone" setMarkerPos _oldpos;
};